package com.alibaba.algo.core.util;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import com.alibaba.algo.core.base.PagerEntity;
import com.alibaba.algo.util.StringUtil;

public class ConvertUtils {

	private static final Logger log = LoggerFactory.getLogger(ConvertUtils.class);

	public ConvertUtils() {
	}

	public static Object poToVo(Class<?> classType, Object po) {
		return copyProperties(classType, po);
	}

	public static List poToVo(Class<?> classType, List poList) {
		return copyProperties(classType, poList);
	}

	public static Object poToVoExclude(Class<?> classType, Object po, List<String> excludeList) {
		return copyProperties(classType, po, StringUtil.join(excludeList, ","));
	}

	/**
	 * @Description: 属性复制-针对PagerEntity
	 * @param: targetClassType 目标对象类型
	 * @author: malin
	 * @date: 2020-12-1 9:37:59
	 */
	public static PagerEntity copyProperties(PagerEntity pagerEntity, Class<?> targetClassType) {
		List<?> list = null;
		if (pagerEntity != null && !CollectionUtils.isEmpty(pagerEntity.getDataList())) {
			list = ConvertUtils.copyProperties(targetClassType, pagerEntity.getDataList());
			pagerEntity.setDataList(list);
		} else {
			pagerEntity.setDataList(null);
		}
		return pagerEntity;
	}

	/**
	 * 属性复制-针对list集合（不支持F里面包含集合的情况）
	 * @author: malin
	 * @date: 2019年10月15日 下午5:04:55
	 */
	public static <F, T> List<T> copyProperties(Class<T> targetClassType, List<F> sourceList) {
		if (CollectionUtils.isEmpty(sourceList)) {
			return new ArrayList<T>();
		}
		long startTime = System.currentTimeMillis();
		ArrayList<T> list = new ArrayList<>(sourceList.size());
		for (F source : sourceList) {
			try {
				T data = targetClassType.newInstance();
				BeanUtils.copyProperties(source, data);
				list.add(data);
			} catch (Exception e) {
				log.error("copy properties error:{}", e.getCause());
				return null;
			}
		}
		long endTime = System.currentTimeMillis();
		log.debug("属性复制耗时：{},{}", endTime - startTime, "毫秒");
		return list;
	}

	/**
	 * 属性复制-针对list集合（不支持F里面包含集合的情况）
	 * @param ignoreProperties 排除不需要复制的字段集合
	 * @author: malin
	 * @date: 2019年10月15日 下午5:12:44
	 */
	public static <F, T> List<T> copyProperties(Class<T> targetClassType, List<F> sourceList,
			String... ignoreProperties) {
		if (CollectionUtils.isEmpty(sourceList)) {
			return new ArrayList<T>();
		}
		long startTime = System.currentTimeMillis();
		ArrayList<T> list = new ArrayList<>(sourceList.size());
		for (Object source : sourceList) {
			try {
				T data = targetClassType.newInstance();
				BeanUtils.copyProperties(source, data, ignoreProperties);
				list.add(data);
			} catch (Exception e) {
				log.error("copy properties error:{}", e.getCause());
				return null;
			}
		}
		long endTime = System.currentTimeMillis();
		log.debug("属性复制耗时：{},{}", endTime - startTime, "毫秒");
		return list;
	}

	/**
	 * 属性复制
	 * @author: malin
	 * @date: 2019年10月15日 下午5:04:55
	 */
	public static <F, T> T copyProperties(Class<T> targetClassType, F source) {
		if (source == null) {
			return null;
		}
		T data = null;
		try {
			data = targetClassType.newInstance();
			BeanUtils.copyProperties(source, data);
		} catch (Exception e) {
			log.error("copy properties error:{}", e.getCause());
			return null;
		}
		return data;
	}

	/**
	 * 属性复制
	 * @author: malin
	 * @date: 2019年10月15日 下午5:04:55
	 */
	public static <F, T> T copyProperties(Class<T> targetClassType, F source, String... ignoreProperties) {
		if (source == null) {
			return null;
		}
		T data = null;
		try {
			data = targetClassType.newInstance();
			BeanUtils.copyProperties(source, data, ignoreProperties);
		} catch (Exception e) {
			log.error("copy properties error:{}", e.getCause());
			return null;
		}
		return data;
	}
}
